package com.beauty.order.dto;

import com.beauty.order.entity.Order;
import com.beauty.order.entity.OrderItem;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Slf4j
public class OrderResponse {
    
    private Long id;
    private String orderNumber;
    private String customerId;
    private BigDecimal totalAmount;
    private BigDecimal shippingCost;
    private BigDecimal taxAmount;
    private Order.OrderStatus status;
    private LocalDateTime orderDate;
    private LocalDateTime paymentDate;
    private LocalDateTime shippingDate;
    private LocalDateTime deliveryDate;
    private String shippingAddress;
    private String billingAddress;
    private String paymentMethod;
    private String shippingMethod;
    private String trackingNumber;
    private String notes;
    private List<OrderItemResponse> orderItems;
    
    public static OrderResponse fromEntity(Order order) {
        OrderResponse response = new OrderResponse();
        response.setId(order.getId());
        response.setOrderNumber(order.getOrderNumber());
        response.setCustomerId(order.getCustomerId());
        response.setTotalAmount(order.getTotalAmount());
        response.setShippingCost(order.getShippingCost());
        response.setTaxAmount(order.getTaxAmount());
        response.setStatus(order.getStatus());
        response.setOrderDate(order.getOrderDate());
        response.setPaymentDate(order.getPaymentDate());
        response.setShippingDate(order.getShippingDate());
        response.setDeliveryDate(order.getDeliveryDate());
        response.setShippingAddress(order.getShippingAddress());
        response.setBillingAddress(order.getBillingAddress());
        response.setPaymentMethod(order.getPaymentMethod());
        response.setShippingMethod(order.getShippingMethod());
        response.setTrackingNumber(order.getTrackingNumber());
        response.setNotes(order.getNotes());
        
        if (order.getOrderItems() != null) {
            response.setOrderItems(order.getOrderItems().stream()
                    .map(OrderItemResponse::fromEntity)
                    .collect(Collectors.toList()));
        }
        
        return response;
    }
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class OrderItemResponse {
        private Long id;
        private String productId;
        private String productName;
        private String productImage;
        private Integer quantity;
        private BigDecimal unitPrice;
        private BigDecimal discountAmount;
        private BigDecimal subtotal;
        private String sku;
        private Map<String, String> options;
        
        private static final ObjectMapper objectMapper = new ObjectMapper();
        
        public static OrderItemResponse fromEntity(OrderItem orderItem) {
            OrderItemResponse response = new OrderItemResponse();
            response.setId(orderItem.getId());
            response.setProductId(orderItem.getProductId());
            response.setProductName(orderItem.getProductName());
            response.setProductImage(orderItem.getProductImage());
            response.setQuantity(orderItem.getQuantity());
            response.setUnitPrice(orderItem.getUnitPrice());
            response.setDiscountAmount(orderItem.getDiscountAmount());
            response.setSubtotal(orderItem.getSubtotal());
            response.setSku(orderItem.getSku());
            
            // 将JSON字符串转换为Map
            if (orderItem.getOptions() != null && !orderItem.getOptions().isEmpty()) {
                try {
                    Map<String, String> optionsMap = objectMapper.readValue(
                            orderItem.getOptions(), 
                            new TypeReference<HashMap<String, String>>() {}
                    );
                    response.setOptions(optionsMap);
                } catch (JsonProcessingException e) {
                    log.error("反序列化options失败", e);
                    response.setOptions(null);
                }
            }
            
            return response;
        }
    }
} 